<?php
/** 
 * 比赛的订阅操作  2015/8/19
 *function:
 *mine        我的关注 
 *scribe 	 关注
 *unscribe    去关
 * @author susy.liang<liangling@ilongyuan.com.cn>
*/

namespace Home\Controller;

class RaceController extends BaseUserController 
{

	/**
	*比赛标识
	*@var int 
	*@access public
	*/
	public $race_id;


	/** 
	 * 订阅比赛
	 * @param $race_id 
	 * @return 
	 */
	
	public function scribe()
	{
		$this->check_param();
		$res = A('Api/RaceScribe', 'Event')->scribe(UID, $this->race_id);
		$res ? $this->success('订阅成功') : $this->error('已经订阅');
	}
	 
	/** 
	 * 取消订阅
	 * @param 
	 * @return 
	 */
	public function unscribe()
	{
		$this->check_param();
		$res = A('Api/RaceScribe', 'Event')->unscribe(UID, $this->race_id);
		$res ? $this->success('取消成功') : $this->error(self::$API_ERROR['HAS_NO_SCRIBE'], '未订阅不能取消');
	}

	/** 
	 * 参数检查
	 * @param 
	 * @return 
	 */
	protected function check_param()
	{
		$race_id = I('id', 0, 'intval');
		if (!$race_id)
		{
			$this->error('参数错误');
		}

		$raceGroup = A('Api/RaceGroup', 'Event')->info($race_id);

		if (!$raceGroup)
		{
			$this->error('非法操作项');
		}

		$this->race_id = $race_id;
	}
		
	/**
	*获取用户订阅的比赛列表
	*/
	public function races(){

		$eventObj = A('Api/RaceScribe', 'Event');

		$race = $eventObj->mine(UID, true);

		if($race){
			$temp = array();
			foreach ($race as $key => $value) {
				uri_format($value['pic']);
				uri_format($value['cover']);
				array_push($temp, $value);
			}
			
		}
		$this->success('', $temp);
	}

}